package XynLeen.GraduationProject.Activities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.classic.adapter.BaseAdapterHelper;

import org.litepal.crud.DataSupport;

import java.util.List;

import XynLeen.GraduationProject.Bean.TypeBean;
import XynLeen.GraduationProject.R;
import XynLeen.GraduationProject.view.BaseRecycleListActivity;

public class Activity_Album extends BaseRecycleListActivity<TypeBean> implements View.OnClickListener {

    private TextView mTvNavigation;
    private ImageButton mIbNavigation_back;
    private ImageButton mIbNavigation_add;

    @Override
    public void doConvert(BaseAdapterHelper helper,final TypeBean item, int position) {

        mTvNavigation=findViewById(R.id.tv_navigation_label);
        mIbNavigation_back=findViewById(R.id.ib_navigation_back);
        mIbNavigation_add=findViewById(R.id.ib_navigation_add);

        mTvNavigation.setText("微相册");
        mIbNavigation_back.setOnClickListener(this);
        mIbNavigation_add.setOnClickListener(this);

        helper.setText(R.id.text,item.getName());
        ImageView imageView=helper.getView(R.id.image);
        Glide.with(this).load(item.getUrl()).into(imageView);
        LinearLayout linearLayout=helper.getView(R.id.item);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Activity_Album.this, Activity_Album_Photo.class);
                intent.putExtra("title", item.getName());
                intent.putExtra("typeid", item.getId()+"");
                startActivity(intent);
            }
        });
        linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                final AlertDialog dialog = new AlertDialog.Builder(Activity_Album.this)
                        .setTitle("提示")//设置对话框的标题
                        .setMessage("确定删除吗？")//设置对话框的内容
                        //设置对话框的按钮
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                DataSupport.delete(TypeBean.class, item.getId());
                                getdata();
                                dialog.dismiss();
                            }
                        }).create();

                dialog.show();
                return false;
            }
        });
    }

    @Override
    public int getAdapterItemId() {
        return R.layout.activity_album_type_item;
    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_recycleview_album;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void onResume() {
        super.onResume();
        getdata();
    }


    public void getdata() {
        List<TypeBean> list = DataSupport.findAll(TypeBean.class);
        mAdapter.clear();
        mAdapter.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void initView() {

    }

    public RecyclerView.LayoutManager getlayoutManager() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        return gridLayoutManager;
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_add, menu);
//        return super.onCreateOptionsMenu(menu);
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//
//        switch (item.getItemId()) {
//            case R.id.add:
//                Intent intent = new Intent(this, Activity_Album_Add.class);
//                intent.putExtra("title", "添加");
//                startActivity(intent);
//                break;
//        }
//        return super.onOptionsItemSelected(item);
//    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_navigation_back:
                finish();
                break;
            case R.id.ib_navigation_add:
                Intent intent1 =new Intent(this,Activity_Album_Add.class);
                intent1.putExtra("title", "添加");
                startActivity(intent1);
                Log.e("tiaozhuan","success");
                finish();
                break;

        }
    }

}
